Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

file-type-stream

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

file-type-stream

Wrapper over file-type that makes using it with streams easier.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

file-type-stream

Build Status

Wrapper over file-type that makes using it with streams easier.

Install

npm install --save file-type-stream

Requires Node v6+

Usage

fileTypeStream(callback)

See ./test directory for usage examples.

import fileTypeStream from 'file-type-stream'
import { PassThrough } from 'stream'

// ...
// s3Client = ....
// ...

const through = new PassThrough()
readStream.pipe(fileTypeStream((type) => {
  // since transform streams buffer up to 16kb which
  // is enough to detect the file type, we get
  // the file type even through through hasn't been piped
  // anywhere yet.

  console.log('ext', type.ext)
  console.log('mime', type.mime)

  // E.g.:
  // We can use that info to open a write stream on aws s3
  // with the correct content type
  client.upload({
    ContentType: type.mime,
    Bucket: 'some-bucket',
    Key: 'some-key',
    Body: through,
  }, null, (err) => {
    // Upload completed!
  })
})).pipe(through)

The code above can be written a bit more intuitively with async/await:


import fileTypeStream from 'file-type-stream'
import { PassThrough } from 'stream'

// ...
// s3Client = ....
// ...

const upload = async (rs) => {
  const Body = new PassThrough()

  const { mime } = await new Promise(resolve => {
    rs.pipe(fileTypeStream(resolve)).pipe(Body)
  })
  const response = client.upload({
    Body,
    ContentType: mime,
    Bucket: 'some-bucket',
    Key: 'some-key',
  }).toPromise()

  console.log(response)
}

FAQs

Package last updated on 30 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc